import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { 
        path: '/', 
        redirect: '/home',
        meta: { title: '首页', isTab: true }
      },
      {
        path: '/home',
        name: 'home',
        component: () => import('../pages/home.vue'),
        meta: { title: '首页', isTab: true }
      },
      {
        path: '/idea',
        name: 'idea',
        component: () => import('../pages/idea.vue'),
        meta: { title: '卡路里目标', isTab: true }
      },
      {
        path: '/add',
        name: 'add',
        component: () => import('../pages/add.vue'),
        meta: { title: '添加', isTab: true }
      },
      {
        path: '/search',
        name: 'search',
        component: () => import('../pages/search.vue'),
        meta: { title: '搜索', isTab: true }
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('../pages/user.vue'),
        meta: { title: '个人中心', isTab: true }
      },
      {
        path: '/user/profile',
        name: 'user-profile',
        component: () => import('../pages/userdetails.vue'),
        meta: { title: '个人信息' }
      },
      {
        path: '/AI',
        name: 'ai',
        component: () => import('../pages/ai.vue'),
        meta: { title: 'AI助手' }
      },
      {
        path: '/login',
        name: 'login',
        component: () => import('../pages/userLogin/login.vue'),
        meta: { title: '登录' }
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('../pages/userLogin/register.vue'),
        meta: { title: '注册' }
      },
      {
        path: '/user/password',
        name: 'user-password',
        component: () => import('../pages/userLogin/repassword.vue'),
        meta: { title: '重置密码' }
      },
      {
        path: '/user/favorite',
        name: 'user-favorite',
        component: () => import('../pages/mystar.vue'),
        meta: { title: '我的收藏' }
      },
      {
        path: '/tongzhi',
        name: 'tongzhi',
        component: () => import('../pages/tongzhi.vue'),
        meta: { title: '通知设置' }
      },
      {
        path: '/recipe-detail/:name',
        name: 'recipe-detail',
        component: () => import('../pages/recipe-detail.vue'),
        meta: { title: '食谱详情' }
      },
      {
        path: '/feedback',
        name: 'feedback',
        component: () => import('../pages/feedback.vue'),
        meta: { title: '意见反馈' }
      },
      {
        path: '/about',
        name: 'about',
        component: () => import('../pages/about.vue'),
        meta: { title: '关于我们' }
      }
  ]
})
router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = '膳小搭-' + title
  }
  // 登录拦截，允许未登录访问/login和/register
  const isLogin = localStorage.getItem('isLogin')
  if (to.path !== '/login' && to.path !== '/register' && !isLogin) {
    next('/login')
  } else {
    // 如果是测试账号，确保用户信息完整
    if (isLogin && localStorage.getItem('userName') === '测试用户') {
      const userEmail = localStorage.getItem('userEmail')
      const userAvatar = localStorage.getItem('userAvatar')
      if (!userEmail) {
        localStorage.setItem('userEmail', 'user')
      }
      if (!userAvatar) {
        localStorage.setItem('userAvatar', 'https://img.yzcdn.cn/vant/cat.jpeg')
      }
    }
    next()
  }
})
export default router